﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using TrangTrai.Data.Utils;
using TrangTrai.Data.Models;
using log4net;

namespace TrangTrai.Data.DAO
{
    public class AoManageDAO
    {
        SqlConnection con;
        SqlCommand command;
        SqlDataReader re=null;
        public static AoManagementDataContext context = new AoManagementDataContext(ConnectionClass.getConnectionString());
        private static readonly ILog log = LogManager.GetLogger(typeof(AoManageDAO).Name);
        public AoManageDAO()
        {
            init();

        }
        private void init()
        {
            con = ConnectionClass.getConnection();
            command = con.CreateCommand();
        }
        //public AoSeasonInfo[] getAoSeasonB(int aoId, int seasonId) {
        //    return context.getAoSeasonByAoAndSeason(aoId, seasonId).ToArray();
        //}
        public void updateCuanHis(int choanId, float percent)
        {
            context.updateCuAnHis(choanId, percent);
        }
        public void updateHoachatHis(int choanId, float percent)
        {
            context.updateHoaChatHis(choanId, percent);
        }
        public AoSeasonEntry getIdByAoAndSeason(int aoId, int seasonId)
        {
            AoSeasonEntry[] arr = context.getAoSeasonId(aoId, seasonId).ToArray();
            if (arr.Length == 0) return null;
            else return arr.First();
        }

        public void TiaAo(TiaAo tiaAo)
        {
            context.DoTiaAo(tiaAo.aoId, tiaAo.seasonId, tiaAo.remain, tiaAo.aoName, tiaAo.ngaytia, tiaAo.note, tiaAo.aoSeasonId);
        }
        public ChoAnLog[] getChoAnLogs(int seasonId, DateTime dt)
        {
            return context.getChoAnLog(seasonId, dt).ToArray();
        }
        public void createAoSeason(int aoId, int seasonId)
        {
            context.createAoSeason(aoId, seasonId);
        }
        public int getAoIdByAoCode(string aocode)
        {
            int aoId = -1;
            try
            {
                
                command.CommandText = "select ao_id as aoId from ao where ao_code=@aoCode";
                command.Parameters.Add(new SqlParameter("@aoCode", aocode));
                re = command.ExecuteReader();
                if (re != null)
                {
                    while (re.NextResult())
                    {
                        aoId = (int)re["aoId"];
                    }
                }
                
            }
            catch (Exception e)
            {

                log.Error("getAoIdByAoCode", e);
            }
            return aoId;

        }
        public void InsertHoaChatHis(HoaChatHisEntry hoachatHis){

            try
            {
                if (con == null || con.State == System.Data.ConnectionState.Closed)
                {
                    con = ConnectionClass.getConnection();
                    command = con.CreateCommand();
                }
                command.CommandText = "insert into hoachat_his(choan_id,hoachat_id,value) values (@choanId,@hoachatId,@value)";
                command.Parameters.Add(new SqlParameter("@choanId", hoachatHis.choanId));
                command.Parameters.Add(new SqlParameter("@hoachatId", hoachatHis.hoachatId));
                command.Parameters.Add(new SqlParameter("@value", hoachatHis.value));
                command.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception e)
            {
                
                log.Error("InsertHoaChatHis", e);
            }
       }
        public void InsertCuAnHis(CuAnHisEntry cuanHis)
        {

            try
            {
                if (con == null || con.State == System.Data.ConnectionState.Closed)
                {
                    con = ConnectionClass.getConnection();
                    command = con.CreateCommand();
                }
                command.CommandText = "insert into cuan_his(choan_id,cuan_id,status_id,actual_amount) values (@choanId,@cuan_id,@status_id,@actual_amount)";
                command.Parameters.Add(new SqlParameter("@choanId", cuanHis.choanId));
                command.Parameters.Add(new SqlParameter("@cuan_id", cuanHis.cuanId));
                command.Parameters.Add(new SqlParameter("@status_id", cuanHis.statusId));
                command.Parameters.Add(new SqlParameter("@actual_amount", cuanHis.actualAmount));
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                log.Error("InsertCuAnHis", e);
                
            }

        }
        public void insertChoAnLog(ChoAnLog logReq)
        {
            try
            {
                context.insertChoAnLog(logReq.tomId, logReq.age, logReq.note, logReq.aoSeasonId, logReq.createdDate, logReq.foodId);
            }
            catch (Exception e)
            {
                log.Error("insertChoAnLog",e);
                
            }
        }
        public CuAnHisEntry[] getCuanHis(int logId) {
            try
            {
                return context.getCuAnHis(logId).ToArray();
            }
            catch (Exception e)
            {
                
                log.Error("getCuanHis",e);
                return null;
            }
        }

        public HoaChatHisEntry[] getHoaChatHis(int logId)
        {
            try
            {
                return context.getHoaChatHis(logId).ToArray();
            }
            catch (Exception e)
            {

                log.Error("getHoaChatHis", e);
                return null;
            }
        }
        public AoDetails getAoDetailsById(int aoId, int seasonId) {
            try
            {
                AoDetails[] temp = context.getAoDetails(aoId, seasonId).ToArray();
                if (temp.Length > 0) return temp.First();
                return null;
            }
            catch (Exception e)
            {
                
                log.Error("getAoDetailsById", e);
                return null;
            }
        }
        public AoEnvDetails getEnvDetails(int aoId, int seasonId) {
            try
            {
                AoEnvDetails[] temp = context.getEnvDetails(aoId, seasonId).ToArray();
                if (temp.Length > 0) return temp.First();
                return null;
            }
            catch (Exception e)
            {
                log.Error("getEnvDetails", e);
                return null;
            }
        
        }
        public int getAoSeasonByAoIdAndSeasonId(int aoId, int seasonId) {
            int aoSeasonId = -1;
            try
            {
                if (con == null || con.State == System.Data.ConnectionState.Closed)
                {
                    con = ConnectionClass.getConnection();
                    command = con.CreateCommand();
                }
                command.CommandText = "getAoSeasonByAoAndSeason";
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@aoId", aoId));
                command.Parameters.Add(new SqlParameter("@seasonId", seasonId));
                SqlDataReader reader = command.ExecuteReader();
                if (reader != null && reader.Read())
                {
                    aoSeasonId = (int)reader[0];
                }
            }
            catch (Exception e)
            {
                 log.Error("getAoSeasonByAoIdAndSeasonId", e);
                
            }
            return aoSeasonId;
        }
    }
}
